package cn.xm.lingshi.service.impl;

import java.util.List;

import cn.xm.lingshi.dao.ShopDao;
import cn.xm.lingshi.dao.impl.ShopDaoImpl;
import cn.xm.lingshi.pojo.Brand;
import cn.xm.lingshi.pojo.Kind;
import cn.xm.lingshi.pojo.PageBean;
import cn.xm.lingshi.pojo.Shop;
import cn.xm.lingshi.pojo.Type;
import cn.xm.lingshi.service.ShopService;

public class ShopServiceImpl implements ShopService {
	private ShopDao dao = new ShopDaoImpl();

	@Override
	public List<Shop> printShopListTop5() {
		try {
			return dao.getShopBySalesTop5();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Shop> printShopListTop6() {
		try {
			return dao.getShopBySalesTop5();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<Type> getType() {
		try {
			return dao.getType();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Kind> getKindByTypeId(int typeId) {
		try {
			return dao.getKindByTypeId(typeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Brand> getBrand() {
		try {
			return dao.getBrand();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int addShop(Shop shop) {
		int i = 0;
		try {
			i = dao.addShop(shop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

	@Override
	public int addShopFlavour(List<Integer> list, int shopid) {
		int i = 0;
		if (!list.isEmpty()) {
			for (Integer flavid : list) {
				try {
					int num = dao.addShopFlavour(flavid, shopid);
					if (num == 1) {
						i++;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		if (i == list.size()) {
			return 1;
		}
		return i;
	}

	@Override
	public int queryTotalRecord(String searchTitle, int searchTId) {
		try {
			return dao.queryTotalRecord(searchTitle, searchTId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int queryTotalPageCount(int pageSize, String searchTitle,
			int searchTId) {
		PageBean<Shop> pageBean = new PageBean<Shop>();
		// 总记录数
		int totalCount = this.queryTotalRecord(searchTitle, searchTId);
		// 设置每页的记录数
		pageBean.setPageSize(pageSize);
		// 计算总页数
		pageBean.setTotalCount(totalCount);
		return pageBean.getTotalPageCount();

	}

	@Override
	public PageBean<Shop> queryNewsByPageNo(int pageNo, int pageSize,
			String searchTitle, int searchTId) {
		PageBean<Shop> pageBean = new PageBean<Shop>();
		try {
			// 设置当前页码
			pageBean.setCurrPageNo(pageNo);
			// 设置每页的记录数
			pageBean.setPageSize(pageSize);
			pageBean.setTotalCount(this
					.queryTotalRecord(searchTitle, searchTId));
			// 根据页码和每页记录数获取偏移量
			int offSet = (pageNo - 1) * pageSize;
			List<Shop> list = dao.queryNewsByOffset(offSet, pageSize,
					searchTitle, searchTId);
			// 设置每页的数据
			pageBean.setList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageBean;
	}
}
